//文件解码
import * as Hex from 'js-base64'
// var ecurve = require('ecurve');
// var Point = ecurve.Point;
// var BigInteger = require('bigi');
// var Curve = ecurve.Curve;
// var assert = require('assert')
const sm2 = require('sm-crypto').sm2
// 获取密钥对
// let keypair = sm2.generateKeyPairHex()
// const publicKey = keypair.publicKey // 公钥
// const privateKey = keypair.privateKey // 私钥
const CRYPTO_NAME_SM2 = "sm2p256v1";
// 和后端约定得密钥对公钥  如公钥字符串前面无04需加上04
const publicKy='04bc176ac05df0212410b0e727d2a2c11e0b82287dc90d213342ee35c52af223d9ec2e9f1e2860107210f9618299d7150592838e8af9b88a81a181840d721ee8f4';
const privateKey='f359b9fbdf82b98629aff8ed4956ad216cbd388214445d87f391a5998cc21e74';
export const ecodeFile = (data) =>{
  const cipherMode = 1
  const result = sm2.doEncrypt(data, publicKy, cipherMode)
  return result
}
// sm-  解密
export const decodeFile= (datas) =>{

  let result=''
  if(datas!=undefined&&datas!=""){

    const buf = Buffer.from(datas, 'utf8')
    const data=buf.toString('utf8')

    console.log('开始解密-----',buf.toString('utf8'))

    const cipherMode = 1
    try {
      result = sm2.doDecrypt(data.slice(2,data.length), privateKey, cipherMode)
    }catch (e) {
      console.log("sd--",e)
    }

    console.log('解密结束-----',result)

  }
  return result
}
export const decodeFile2= (datas) =>{

  let result=''
  if(datas!=undefined&&datas!=""){

    const buf = Buffer.from(datas, 'utf8')
    const data=buf.toString('utf8')

    console.log('开始解密-----',buf.toString('utf8'))

    const cipherMode = 1
    try {
      result = sm2.doDecrypt(data, privateKey, cipherMode)
    }catch (e) {
      console.log("sd--",e)
    }

    console.log('解密结束-----',result)

  }
  return result
}
